package com.cjx.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cjx.util.GetConnection;
import com.cjx.util.GetData;
import com.cjx.util.SetData;

/**
 * Servlet implementation class addServlet
 */
@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 *  AC操作：
		 *  1、add表示新增一轮按钮
		 *  2、addSave表示保存新增表单按钮
		 *  3、groupList表示局列表
		 *  4、addGroupForm表示跳转局详情
		 *  5、addGroupFormSave表示保存局详情
		 */
		request.setCharacterEncoding("utf-8");
	    response.setCharacterEncoding("utf-8");
	    response.setContentType("textml;charset=UTF-8");
	    
		//数据库连接
		GetConnection conn = new GetConnection();
		Connection con = conn.getConn();
		
		String AC = request.getParameter("AC");
		
		if("add".equals(AC)){
			request.getRequestDispatcher("/addform.jsp").forward(request,response);
		
		}else if("addSave".equals(AC)){
			
			//获取新增表单的输入值;
			String playera = request.getParameter("playera");
			String playerb = request.getParameter("playerb");
			String playerc = request.getParameter("playerc");
			String score1 = request.getParameter("score1");
			String score2 = request.getParameter("score2");
			String score3 = request.getParameter("score3");
			
			HashMap map = new HashMap();
			map.put("playera", playera);
			map.put("playerb", playerb);
			map.put("playerc", playerc);
			map.put("score1", score1);
			map.put("score2", score2);
			map.put("score3", score3);
			
			//调用插入数据库的类
			SetData sd = new SetData();
			sd.setRoundinfo(map);
			
			//跳回首页
			request.getRequestDispatcher("/index.jsp").forward(request,response);
		
		}else if("groupList".equals(AC)){
			//跳转局列表
			request.getRequestDispatcher("/groupinfolist.jsp?roundid="+request.getParameter("roundid")).forward(request,response);
		
		}else if("addGroupForm".equals(AC)){
			//跳转局详情
			request.getRequestDispatcher("/groupinfoform.jsp").forward(request,response);
					
		}else if("addGroupFormSave".equals(AC)){
			
			//获取新增局表单的输入值;
			String landlord = request.getParameter("landlord");
			String winner = request.getParameter("winner");
			String score = request.getParameter("score");
			String bomb = request.getParameter("bomb");
			
			HashMap map = new HashMap();
			map.put("roundid", request.getParameter("roundid"));
			map.put("landlord", landlord);
			map.put("winner", winner);
			map.put("score", score);
			map.put("bomb", bomb);
			
			//调用插入数据库的类
			SetData sd = new SetData();
			sd.setGroupinfo(map);
			
			//跳转局列表
			request.getRequestDispatcher("/groupinfolist.jsp?roundid="+request.getParameter("roundid")).forward(request,response);
			
		}else if("total".equals(AC)){
			//调用插入数据库的类
			SetData sd = new SetData();
			sd.setStatistics(request.getParameter("roundid"));
			
			//跳转局列表
			request.getRequestDispatcher("/totalinfo.jsp?roundid="+request.getParameter("roundid")).forward(request,response);
		}else if("totalinfo".equals(AC)){
			//跳转局列表
			request.getRequestDispatcher("/totalinfo.jsp?roundid="+request.getParameter("roundid")).forward(request,response);
		}
	}
}
